#include<iostream>
#include<queue>
#include<vector>
using namespace std;

int n;
int d[10005];
vector<int> v[10005];
int cur, ans[10005];

void topologicalSort()
{
    queue<int> q;
    for (int i = 1; i <= n; i++)
    if (d[i] == 0) q.push(i);
    while(!q.empty())
    {
        int f = q.front();
        ans[++cur] = f;
        q.pop();
        for (int i = 0; i < v[f].size(); i++)
        {
            int t = v[f][i];
            d[t]--;
            if (d[t] ==0)
            q.push(t);
        }
    }
}

int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        int x,y;
        cin >> x >> y;
        d[y]++;
        v[x].push_back(y);
    }
    topologicalSort();
    if (cur != n)
    {
        cout << "图中有圈，无法进行拓扑排序！！！" << endl;
    }
    else
    {
        cout << "拓扑排序后的序列：";
        for (int i = 1; i <= cur; i++)
        printf("%d", ans[i]);
        puts("");
    }
    return 0;
}